<div class="content-section introduction">
    <div>
        <span class="feature-title">Drag and Drop</span>
        <span>pDraggable and pDroppable directives apply drag-drop behaviors to any element.</span>
    </div>
</div>

<div class="content-section implementation">
    <h3 class="first">Drag Only</h3>
    <div pDraggable="pnl"  dragHandle=".ui-panel-titlebar">
        <p-panel header="Drag Header">
            The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding. 
            His beloved son Michael has just come home from the war, but does not intend to become part of his father's business. 
            Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, 
            kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
        </p-panel>
    </div>
    
    <h3>Drag and Drop to DataTable</h3>
    <div class="ui-grid ui-grid-pad ui-grid-responsive">
        <div class="ui-grid-row">
            <div class="ui-grid-col-6 ui-widget-content" style="border-right:0 none">
                <ul style="margin:0;padding:0">
                    <li *ngFor="let car of availableCars" class="ui-state-default ui-helper-clearfix" pDraggable="cars"
                        (onDragStart)="dragStart($event,car)" (onDragEnd)="dragEnd($event)">
                        <i class="fa fa-arrows fa-2x" style="float:right;margin-top:8px"></i>
                        <img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="float:left" draggable="false">
                        <div style="margin:8px 0 0 8px;float:left">{{car.vin}} - {{car.year}}</div>
                    </li>
                </ul>
            </div>
            <div class="ui-grid-col-6 ui-widget-content" pDroppable="cars" (onDrop)="drop($event)"
                    [ngClass]="{'ui-state-highlight':draggedCar}">
                <p-dataTable [value]="selectedCars">
                    <p-column field="vin" header="Vin"></p-column>
                    <p-column field="year" header="Year"></p-column>
                    <p-column field="brand" header="Brand"></p-column>
                    <p-column field="color" header="Color"></p-column>
                </p-dataTable>
            </div>
        </div>
    </div>
</div>

<div class="content-section documentation">
    <p-tabView effect="fade">
        <p-tabPanel header="Documentation">
            <h3>Import</h3>
<pre>
<code class="language-typescript" pCode ngNonBindable>
import &#123;DragDropModule&#125; from 'primeng/primeng';
</code>
</pre>

            <h3>Getting Started</h3>
            <p>pDraggable and pDroppable are attached to a target element to add drag-drop behavior. The value of a Directive attribute is required 
            and it defines the scope to match draggables with droppables. Droppable scope can also be an array to accept multiple droppables.</p>
            
<pre>
<code class="language-markup" pCode ngNonBindable>
&lt;div pDraggable="dd"&gt;Draggable Div&lt;/div&gt;

&lt;div pDroppable="dd"&gt;Droppable Div&lt;/div&gt;
</code>
</pre>

            <h3>Drag Handle</h3>
            <p>Drag handle is used to restrict dragging unless mousedown occurs on the specified element. Panel below can only be dragged using its header.</p>

            <pre>
<code class="language-markup" pCode ngNonBindable>
&lt;div pDraggable="pnl"  dragHandle=".ui-panel-titlebar"&gt;
    &lt;p-panel header="Drag Header"&gt;
        The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding. 
        His beloved son Michael has just come home from the war, but does not intend to become part of his father's business. 
        Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, 
        kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
    &lt;/p-panel&gt;
&lt;/div&gt;
</code>
</pre>


            <h3>Draggable</h3>
            <h4>Attributes</h4>
            <div class="doc-tablewrapper">
                <table class="doc-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>dragEffect</td>
                            <td>string</td>
                            <td>null</td>
                            <td>Defines the cursor style, valid values are none, copy, move, link, copyMove, copyLink, linkMove and all.</td>
                        </tr>
                        <tr>
                            <td>dragHandle</td>
                            <td>string</td>
                            <td>null</td>
                            <td>Selector to define the drag handle, by default anywhere on the target element is a drag handle to start dragging.</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <h4>Events</h4>
            <div class="doc-tablewrapper">
                <table class="doc-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Parameters</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>onDragStart</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when drag begins.</td>
                        </tr>
                        <tr>
                            <td>onDrag</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke on dragging.</td>
                        </tr>
                        <tr>
                            <td>onDragEnd</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when drag ends.</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <h3>Droppable</h3>
            <h4>Attributes</h4>
            <div class="doc-tablewrapper">
                <table class="doc-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>dropEffect</td>
                            <td>string</td>
                            <td>null</td>
                            <td>Defines the cursor style on drag over, valid values are copy, move, link and move.</td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <h4>Events</h4>
            <div class="doc-tablewrapper">
                <table class="doc-table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Parameters</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>onDragEnter</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when a draggable enters drop area.</td>
                        </tr>
                        <tr>
                            <td>onDragOver</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when an element or text selection is being dragged over a valid drop target (every few hundred milliseconds).</td>
                        </tr>
                        <tr>
                            <td>onDrop</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when a draggable is dropped onto drop area.</td>
                        </tr>
                        <tr>
                            <td>onDragLeave</td>
                            <td>
                                event: browser event
                            </td>
                            <td>Callback to invoke when a draggable leave drop area.</td>
                        </tr>
                    </tbody>
                </table>
            </div>


            <h3>Dependencies</h3>
            <p>Native HTML5 DragDrop.</p>
        </p-tabPanel>
        <p-tabPanel header="Source">
            <a href="https://github.com/primefaces/primeng/tree/master/src/app/showcase/components/dragdrop" class="btn-viewsource" target="_blank">
                <i class="fa fa-github"></i>
                <span>View on GitHub</span>
            </a>
<pre>
<code class="language-markup" pCode ngNonBindable>
&lt;h3 class="first"&gt;Drag Only&lt;/h3&gt;
&lt;div pDraggable="pnl"  dragHandle=".ui-panel-titlebar"&gt;
    &lt;p-panel header="Drag Header"&gt;
        The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding. 
        His beloved son Michael has just come home from the war, but does not intend to become part of his father's business. 
        Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family, 
        kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
    &lt;/p-panel&gt;
&lt;/div&gt;

&lt;h3&gt;Drag and Drop to DataTable&lt;/h3&gt;
&lt;div class="ui-grid ui-grid-pad ui-grid-responsive"&gt;
    &lt;div class="ui-grid-row"&gt;
        &lt;div class="ui-grid-col-6 ui-widget-content" style="border-right:0 none"&gt;
            &lt;ul style="margin:0;padding:0"&gt;
                &lt;li *ngFor="let car of availableCars" class="ui-state-default ui-helper-clearfix" pDraggable="cars"
                    (onDragStart)="dragStart($event,car)" (onDragEnd)="dragEnd($event)"&gt;
                    &lt;i class="fa fa-arrows fa-2x" style="float:right;margin-top:8px"&gt;&lt;/i&gt;
                    &lt;img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="float:left" draggable="false"&gt;
                    &lt;div style="margin:8px 0 0 8px;float:left"&gt;{{car.vin}} - {{car.year}}&lt;/div&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class="ui-grid-col-6 ui-widget-content" pDroppable="cars" (onDrop)="drop($event)"
                [ngClass]="&#123;'ui-state-highlight':draggedCar&#125;"&gt;
            &lt;p-dataTable [value]="selectedCars"&gt;
                &lt;p-column field="vin" header="Vin"&gt;&lt;/p-column&gt;
                &lt;p-column field="year" header="Year"&gt;&lt;/p-column&gt;
                &lt;p-column field="brand" header="Brand"&gt;&lt;/p-column&gt;
                &lt;p-column field="color" header="Color"&gt;&lt;/p-column&gt;
            &lt;/p-dataTable&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code>
</pre>

<pre>
<code class="language-typescript" pCode ngNonBindable>
export class DragDropDemo &#123;
    
    availableCars: Car[];
    
    selectedCars: Car[];
    
    draggedCar: Car;
    
    constructor(private carService: CarService) &#123; &#125;
    
    ngOnInit() &#123;
        this.selectedCars = [];
        this.carService.getCarsSmall().then(cars => this.availableCars = cars);
    &#125;
    
    dragStart(event,car: Car) &#123;
        this.draggedCar = car;
    &#125;
    
    drop(event) &#123;
        if(this.draggedCar) &#123;
            let draggedCarIndex = this.findIndex(this.draggedCar);
            this.selectedCars = [...this.selectedCars, this.draggedCar];
            this.availableCars = this.availableCars.filter((val,i) => i!=draggedCarIndex);
            this.draggedCar = null;
        &#125;
    &#125;
    
    dragEnd(event) &#123;
        this.draggedCar = null;
    &#125;
    
    findIndex(car: Car) &#123;
        let index = -1;
        for(let i = 0; i < this.availableCars.length; i++) &#123;
            if(car.vin === this.availableCars[i].vin) &#123;
                index = i;
                break;
            &#125;
        &#125;
        return index;
    &#125;

&#125;
</code>
</pre>
        </p-tabPanel>
    </p-tabView>
</div>